package com.by.service.impl;

import com.by.common.bean.PageBean;
import com.by.mapper.AdminMapper;
import com.by.pojo.dto.AdminPageQueryDTO;
import com.by.pojo.entity.Admin;
import com.by.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    private AdminMapper adminMapper;


    /**
     * 添加管理员
     *
     * @param admin
     * @return
     */
    @Override
    public int save(Admin admin) {
        return adminMapper.save(admin);
    }

    @Override
    public int update(Admin admin) {
        if (admin.getId() != null) {
            return adminMapper.update(admin);
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        if (id != null) {
            return adminMapper.delete(id);
        }
        return 0;
    }

    /**
     * 分页查询管理员
     *
     * @param adminPageQueryDTO
     * @return
     */
    @Override
    public PageBean query(AdminPageQueryDTO adminPageQueryDTO) {
        int page = 1;
        int pageSize = 10;
        if(adminPageQueryDTO.getPage()!=null) page = adminPageQueryDTO.getPage();
        if(adminPageQueryDTO.getPageSize()!=null) pageSize = adminPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        List<Admin> list = adminMapper.query(adminPageQueryDTO);
        PageInfo info = new PageInfo(list);
        return new PageBean(page, pageSize, info.getTotal(), list);
    }

    @Override
    public Admin selectById(Integer id) {
        if (id != null) {
            return adminMapper.selectById(id);
        }
        return null;
    }

}
